/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdIHObject.h"
#include "hhdIGameObjectTreeHolder.h"
#include "hhdIMessageSender.h"
#include "hhdTable.hpp"
#include "hhdArrayList.hpp"
#include "hhdRegistry.h"

namespace hhd
{
	class IComponent;
	class IDirector;
	class GameObject;
	class GUIPanel;
	class GameTimer;

    enum ScenePhase
    {
        None,
        Loading,
        Initializing,
        GameObjectUpdate,
        DirectorUpdate,
        MessageHandling,
        Draw
    };

    enum SceneProcessesCompleted
    {
        SPC_NONE			= 0x00,
        SPC_LOADED			= 0x01,
        SPC_INITIALIZED		= 0x02,
        SPC_HAS_BEEN_RUN	= 0x04
    };

    class TScene : public IGameObjectTreeHolder, public IMessageSender
    {
        ContentH(hhd::TScene, TScene);
		HHD_REGISTRANT_H(IContent, TScene);

    public:
        TScene();
        ~TScene();

        inline ScenePhase getScenePhase() const
        {
            return _scene_phase;
        }

        // called whenever the table dirty flag is set
        void buildGameObjectTable();

        // called by game on each frame
        void update(const GameTimer& in_gt);

        // draw directors which are in the draw list
        void draw();

        // add a director to this scene
        void addDirector(IDirector* in_director, bool is_drawable=false);

        // get a director pointer by type
        IDirector* getDirector(const String& in_name);

        // add a named game object to this scene
        void addGameObject(GameObject* in_game_object);

        /* get back a game object by it's name */
        GameObject* getGameObject(const String& in_name);

        /*
        * Called when we need a list of the GameObjects in the Scene with a given name
        * @return Returns a pointer to the list of the GameObjects in the Scene with a given name if there
        * are any, otherwise it returns NULL.
        */
        Vector<GameObject*>* getGameObjects(const String& in_name);

        // remove a game object from the scene by it's global name
        void RemoveReleaseGameObject(GameObject* in_to_remove);

        // add a named gui panel to this scene
        void addGUIPanel(GUIPanel* in_gui_panel);

        // get back a gui panel by it's name
        GUIPanel* getGUIPanel(const String& in_name);

        // set the root game object
        void setRootGameObject(GameObject* in_game_object);

        // get the root game object
        GameObject* getRootGameObject();

        // set the root gui panel
        void setRootGUIPanel(GUIPanel* in_panel);

        // get the root game object
        GUIPanel* getRootGUIPanel();

        // load a scene from an XML file and return it (uninitialized)
        static TScene* loadScene(const String& in_filename);

        // initialize the scene
        void initialize();

        //load a prefab
        //Prefab* getPrefab(const String& in_path);
        //GameObject* getPrefabInstance(const String& in_name, GameObject* in_to_be_parent = 0);
        //GameObject* getPrefabLoadInstance(const String& in_name, const String& in_path, GameObject* in_to_be_parent);


        // get the pointer to the game project
        IGame* getGame();

        // register director for game messages
        void registerForMessages(IDirector* in_director);

        // register component for game messages
        void registerForMessages(IComponent* in_component);

        // notify registered objects that scene is being paused
        void pause();

        // notify registered object that scene is being resumed
        void resume();

        void setDirtyLayer(int);

    private:
        friend class IGame;
        ScenePhase _scene_phase;
        int _scene_processes_completed;

        IGame* _parent_game;
        GameObject* _root_game_object;
        GUIPanel* _root_gui_panel;
        // the table where we store our game objects for updating (each row is a level in the tree)
        Table<GameObject*> _game_object_table;
        // buffer to store our game objects in when putting in the table
        ArrayList<GameObject*> _game_object_buffer;
        bool _table_dirty;
        int _table_dirty_layer;

        Vector<IDirector*> _directors;
        HashTable<String, IDirector*> _director_map;

        /* A mapping from name (string) to GameObjects with that name. */
        HashTable<String, Vector<GameObject*> > _game_object_map;

        //UNKNOWN USE
        Vector<IComponent*> _messaged_components;
        Vector<IDirector*> _messaged_directors;

        //a hash table of all prefabs this scene cares about,
        //key is the path to the ContentXML
        HashTable<String, TPrefab*> _path_to_prefabs;

        //hash table for the GUI Panels
        HashTable<String, GUIPanel*> _gui_panel_map;
    };
}
